﻿package com.tomvian.maths.areas
{
	import com.tomvian.maths.Vector2D;
	
	/**
	 * CircleArea
	 * @author Thomas Vian
	 */
	public class CircleArea extends Area
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		private var _center:Vector2D;
		private var _radius:Number;
		private var _radiusSqr:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function CircleArea(center:Vector2D, radius:Number)
		{
			_center = center;
			_radius = radius;
			_radiusSqr = radius * radius;
			
			area = Math.PI * _radiusSqr;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function contains(vector:Vector2D):Boolean 
		{ 
			return vector.subtract(_center).lengthSqr <= _radiusSqr;
		}
		
		override public function random():Vector2D 
		{
			var rand:Number = Math.random();
			return Vector2D.newFromAngle(Math.random() * Math.PI * 2, (1 - rand*rand) * _radius).addMod(_center);
		}
	}
}